<?php
namespace Home\Model;
use Think\Model;
//goods表操作类
class GoodsModel extends Model{
    //-数据查询方法
   
   //详情猜你喜欢
    public function goods_like(){
        $list = $this->limit(3)->order('rand()')->select();
        return $list;
    } 
    //首页猜你喜欢
    public function goods_like2(){
         $list = $this->limit(5)->order('rand()')->select();
        return $list;
    }
    //热卖商品
    public function goods_hot(){
        //随机出四个商品
        $list = $this->limit(4)->order('rand()')->select();
        return $list;
    }
    //手机模块
    public function goods_phone(){
        //设置查询条件
        $map['name'] = ['eq','手机'];
        $type= $this->table('zx_type')->field('id')->where($map)->find();
        $id = $type['id'];
        $map = [];
        $map['path'] = ['like',"%$id%"];
        // return $id;
        // 查询出所有子类名字
        $name = $this->table('zx_type')->field('name')->where($map)->select();
        // return $name;
        $map = [];
        foreach($name as $key=>$val){
            $map[] = $val['name'];
        }
        // return $map;
        $m['typeid'] = ['in',$map];
        // return array('华为','荣耀');
        $phone = $this->where($m)->limit(10)->order('price')->select();
        return [
            'phone'=>$phone,
            'name'=>$name
        ];
    }
    //查询电脑类方法
    public function goods_computer(){
        //设置查询条件
        $map['name'] = ['eq','电脑'];
        $type= $this->table('zx_type')->field('id')->where($map)->find();
        $id = $type['id'];
        $map = [];
        $map['path'] = ['like',"%$id%"];
        // return $id;
        // 查询出所有子类名字
        $name = $this->table('zx_type')->field('name')->where($map)->select();
        // return $name;
        $map = [];
        foreach($name as $key=>$val){
            $map[] = $val['name'];
        }
        // return $map;
        $m['typeid'] = ['in',$map];
        // return array('华为','荣耀');
        $computer = $this->where($m)->limit(10)->order('price')->select();
        return [
            'computer'=>$computer,
            'name'=>$name
        ];
    }
    //查询智能家居方法
    public function goods_smart(){
        //设置查询条件
        $map['name'] = ['eq','智能家居'];
        $type= $this->table('zx_type')->field('id')->where($map)->find();
        $id = $type['id'];
        $map = [];
        $map['path'] = ['like',"%$id%"];
        // return $id;
        // 查询出所有子类名字
        $name = $this->table('zx_type')->field('name')->where($map)->select();
        // return $name;
        $map = [];
        foreach($name as $key=>$val){
            $map[] = $val['name'];
        }
        // return $map;
        $m['typeid'] = ['in',$map];
        // return array('华为','荣耀');
        $smart = $this->where($m)->limit(10)->order('price')->select();
        return [
            'smart'=>$smart,
            'name'=>$name
        ];
    }
    //查询手机配件类商品
    public function goods_acc(){
         //设置查询条件
        $map['name'] = ['eq','手机配件'];
        $type= $this->table('zx_type')->field('id')->where($map)->find();
        $id = $type['id'];
        $map = [];
        $map['path'] = ['like',"%$id%"];
        // return $id;
        // 查询出所有子类名字
        $name = $this->table('zx_type')->field('name')->where($map)->select();
        // return $name;
        $map = [];
        foreach($name as $key=>$val){
            $map[] = $val['name'];
        }
        // return $map;
        $m['typeid'] = ['in',$map];
        // return array('华为','荣耀');
        $acc = $this->where($m)->limit(10)->order('price')->select();
        return [
            'acc'=>$acc,
            'name'=>$name
        ];
    }
    
    //===20170124==实现搜索商品功能==============================================
    public function seach_goods()
    {
        $data = I('get.seach');
        if(!empty(trim($data))){
            $where['goods'] = ['like',"%$data%"]; 
        }
        $count = $this->where($where)->count();
        $page = new \Think\Page($count,3);
        $show = $page->show();
        $list = $this->where($where)->limit($page->firstRow.','.$page->listRows)->select();

        return ['glist'=>$list,'show'=>$show];
    }
}